package com.zxl.diy.weather.model.observable;

import android.location.Location;

import com.zxl.diy.weather.model.PheadHelper;
import com.zxl.diy.weather.model.bean.City;
import com.zxl.manager.privacy.utils.ApplicationHelper;
import com.zxl.manager.privacy.utils.model.Observable;
import com.zxl.manager.privacy.utils.utils.HttpUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 城市信息查询
 * Created by Administrator on 2016/11/5.
 */
public class QueryCityInfoObservable extends Observable<City, Integer, List<City>> {

    private static final int FLAG_CITY_ID = 0;

    private static final int FLAG_KEY_WORD = 1;

    private static final int FLAG_LOCATION = 2;

    private int mFlag;

    private String mCityId;

    private String mKeyWord;

    private Location mLocation;

    private QueryCityInfoObservable(int mFlag, String mCityId, String mKeyWord, Location mLocation) {
        this.mFlag = mFlag;
        this.mCityId = mCityId;
        this.mKeyWord = mKeyWord;
        this.mLocation = mLocation;
    }

    /**
     * 通过关键词查询城市信息
     * @param keyWord
     * @return
     */
    public static QueryCityInfoObservable createQueryCityByKeyWord(String keyWord) {
        return new QueryCityInfoObservable(FLAG_KEY_WORD, null, keyWord, null);
    }

    /**
     * 通过城市ID获取城市信息
     * @param cityId
     * @return
     */
    public static QueryCityInfoObservable createQueryCityByCityId(String cityId) {
        return new QueryCityInfoObservable(FLAG_CITY_ID, cityId, null, null);
    }

    /**
     * 通过位置查询城市信息
     * @param location
     * @return
     */
    public static QueryCityInfoObservable createQueryCityByLocation(Location location) {
        return new QueryCityInfoObservable(FLAG_LOCATION, null, null, location);
    }

    @Override
    public void run() {
        String url = PheadHelper.BASE_SERVICE_URL;
        Map<String, String> header = PheadHelper.creatPheadHelp(ApplicationHelper.getContext());
        switch (mFlag) {
            case FLAG_CITY_ID:
                url += "city/getCity";
                header.put("cityId", mCityId);
                break;
            case FLAG_KEY_WORD:
                url += "city/search";
                header.put("k", mKeyWord);
                break;
            case FLAG_LOCATION:
                url += "city/gps";
                header.put("latlng", mLocation.getLatitude() + "," + mLocation.getLongitude());
                break;
        }
        byte[] bytes = HttpUtils.doGet(url, header);
        if (null != bytes && bytes.length > 0) {
            try {
                JSONObject jsonObject = new JSONObject(new String(bytes, "utf-8"));
                if (PheadHelper.isSuccessFromService(jsonObject)) {
                    List<City> list = new ArrayList<>();
                    JSONArray jsonArray = jsonObject.optJSONArray("cities");
                    for (int i = 0; i < jsonArray.length(); i++) {
                        City city = new City();
                        city.parseJson(jsonArray.optJSONObject(i));
                        list.add(city);
                        handleSuccess(city);
                    }
                    handleComplete(list);
                    return;
                } else {
                    handleFail(0);
                }
            } catch (Exception e) {
                e.printStackTrace();
                handleFail(-1);
            }
            handleComplete(null);
        }
    }
}
